//
//  VEAssociationListViewController.m
//  VEMoneyPorject
//
//  Created by BlueWind on 2018/9/6.
//  Copyright © 2018年 陈建. All rights reserved.
//

#import "VEAssociationListViewController.h"
#import "VETAssociationTableViewCell.h"
#import "VEAssociationDetailViewController.h"
#import "DTLogInViewController.h"


@interface VEAssociationListViewController ()

@end

@implementation VEAssociationListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    // 可以刷新可以加载更多
    self.refreshType = CJBaseTableVcRefreshTypeRefreshAndLoadMore;
    [self cj_refresh];
}


- (void)cj_refresh{
    [super cj_refresh];
    [self loadData];
}

- (void)loadData{
    [super loadData];
    [self requestDataWithType:1];
    
}

- (void)cj_loadMore{
    [super cj_loadMore];
    NSLog(@"%ld",self.page);
    [self requestDataWithType:2];
    
}

- (void)requestDataWithType:(NSInteger)type{
    
    NSDictionary *dic = [NSDictionary new];
    NSString *url = @"";
    
    NSString *userId = [[CJUserInfoManager sharedManager] currentUserInfo].USERID;
    
    if ([NSObject isNilOrNull:userId]) {
        userId = @"";
    }
    
    if (self.indexType == 1) { // 热门
        dic = @{@"Type":@"0",
                @"Page":@(self.page),
                @"Userid":userId
                };
    } else if (self.indexType == 2) { // 推荐
        dic = @{@"Type":@"1",
                @"Page":@(self.page),
                @"Userid":userId
                };
    } else if (self.indexType == 3) { // 其他
        dic = @{@"Type":@"2",
                @"Page":@(self.page),
                @"Userid":userId
                };
    }
    url = [NSString stringWithFormat:@"%@/VEService/GetAPPSQList.do",kPublicInterfaceAPI];
    
    CJBaseRequest *request = [CJBaseRequest cj_requestWithUrl:url isPost:YES withParameters:dic];
    [request cj_sendRequestWithCompletion:^(id response, BOOL success, NSString *message) {
        [self cj_endRefresh];
        [self cj_endLoadMore];
        self.isSuccessNet = YES;
    
        if (![NSObject isNilOrNull:response]) {
            NSArray *arr = response;
            if (arr.count == 0) {
                self.page -=1;
                return;
            }
            if (type == 1) {
                [self.dataArray removeAllObjects];
                self.dataArray = [VEAssionModel mj_objectArrayWithKeyValuesArray:arr];
            } else {
                NSArray *arr1 = [VEAssionModel mj_objectArrayWithKeyValuesArray:arr];
                [self.dataArray addObjectsFromArray:arr1];
            }
            
            
            [self.tableView reloadData];
        }
        
    }];
}

- (NSInteger)cj_numberOfSections {
    return 1;
}

/**
 某个cell的数量
 */
- (NSInteger)cj_numberOfRowsInsection:(NSInteger)section {
    return self.dataArray.count;
}

- (CGFloat)cj_cellHeightAtIndexPath:(NSIndexPath *)indexPath {
    return 85;
}

/**
 某行的cell
 */
- (CJBaseTableViewCell *)cj_cellAtIndexPath:(NSIndexPath *)indexPath {
    
    VETAssociationTableViewCell *cell =[VETAssociationTableViewCell cellWithTableView:self.tableView];
    cell.isHadJoin = NO;
    cell.model = self.dataArray[indexPath.row];
    cell.isCrearDT = self.isCrearDT;
    WEAKSELF
    cell.jionAssionAction = ^{
        
        VEAssionModel *model = self.dataArray[indexPath.row];
        if (self.isCrearDT == YES) {

            
            if (self->_seletedAssionGetAssionId) {
                self->_seletedAssionGetAssionId(model);
            }
            [weakSelf.navigationController popViewControllerAnimated:YES];

        } else {
            if (![[CJUserInfoManager sharedManager] isLogin]) {
                DTLogInViewController *vc = [[DTLogInViewController alloc] init];
                vc.loginSuccessBlock = ^{
                };
                [self pushVc:vc];
            } else {
                NSString *userId = [[CJUserInfoManager sharedManager] currentUserInfo].USERID;

                if ([NSObject isNilOrNull:userId]) {
                    userId = @"";
                }
                
                CJBaseRequest *request = [CJBaseRequest cj_requestWithUrl:[NSString stringWithFormat:@"%@/VEService/JoinShequn.do",kPublicInterfaceAPI] isPost:YES withParameters:@{@"shequnid":model.COMMUNITYID,@"Userid":userId}];
                [request cj_sendRequestWithCompletion:^(id response, BOOL success, NSString *message) {
                    
                    [CJUIKit showSuccessWithStatus:message];
                    
                    if ([message isEqualToString:@"加入成功！"]) {
                        model.ZHUANGTAI = @"已加入";
                        [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimationNone)];
                    }

                }];
            }
        }
        

    };
    return cell;
    
}

- (void)cj_didSelectCellAtIndexPath:(NSIndexPath *)indexPath cell:(CJBaseTableViewCell *)cell{
    VEAssociationDetailViewController *vc = [VEAssociationDetailViewController new];
    VEAssionModel *model = self.dataArray[indexPath.row];
    vc.assionId = model.COMMUNITYID;
    vc.isJoin = model.ZHUANGTAI;
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)jionAssion {
    
}

@end
